﻿using System.Threading.Tasks;

namespace winS.Unity
{
    /// <summary>
    /// 控制单元. 负责 <see cref="Controller"/> 下具体功能的实现
    /// </summary>
    public class ControlUnit
    {
        public bool enabled { get; private set; }

        /// <summary>
        /// 标记控制单元是否可以被禁用.
        /// 如果该值返回 false, 则该控制单元在启用后无法被禁用
        /// </summary>
        protected virtual bool disableable => true;

        protected internal Controller controller { get; internal set; }

        private bool released;

        /// <summary>
        /// 启用控制单元
        /// </summary>
        public void Enable()
        {
            if (enabled || released) return;
            enabled = true;
            OnEnable();
        }
        /// <summary>
        /// 禁用控制单元
        /// </summary>
        public void Disable()
        {
            if (!enabled || released || !disableable) return;
            enabled = false;
            OnDisable();
        }
        /// <summary>
        /// 重启控制单元
        /// </summary>
        public void Reenable()
        {
            Disable();
            Enable();
        }
        public void Release()
        {
            Disable();
            enabled = false;
            released = true;
            OnRelease();
        }

        protected virtual void OnEnable()
        {
        }
        protected virtual void OnDisable()
        {
        }
        protected virtual void OnRelease()
        {
        }
    }
}
